from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from loguru import logger
import uvicorn
from app.api.api import api_router
from app.core.config import settings

# 创建FastAPI应用实例
app = FastAPI(
    title="混元智能体API",
    description="混元智能体Web应用后端API服务",
    version="0.1.0"
)

# 配置CORS中间件
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # 在生产环境中需要设置具体的域名
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 配置日志
logger.add("logs/api.log", rotation="500 MB", level="INFO")

# 注册API路由
app.include_router(api_router, prefix=settings.API_V1_STR)

@app.get("/")
async def root():
    """
    API根路由，用于测试服务是否正常运行
    """
    return {
        "message": "欢迎使用混元智能体API服务",
        "status": "running",
        "version": "0.1.0"
    }

if __name__ == "__main__":
    # 启动服务器
    uvicorn.run(
        "main:app",
        host="0.0.0.0",
        port=8000,
        reload=True,  # 开发模式下启用热重载
        workers=1
    ) 